package com.budwk.app.util.adminUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * @program: dfxy-community
 * @description: 获取IdCard信息工具
 * @author: LJT
 * @create: 2024-04-30 08:41
 **/
public class IdCardUtil {
    /**
     * 男/女/未知 转换成 1/0/2
     *
     * @param gender 男/女
     * @return
     */
    public static String getGenderNum(String gender) {
        if ("男".equals(gender)) {
            return "1";
        } else if ("女".equals(gender)) {
            return "0";
        } else {
            return "2";
        }
    }

    /**
     * 1/0/2 转换成 男/女/未知
     *
     * @param gender
     * @return
     */
    public static String getGenderByNum(String gender) {
        if ("1".equals(gender)) {
            return "男";
        } else if ("0".equals(gender)) {
            return "女";
        } else {
            return "未知";
        }
    }

    /**
     * 根据身份证倒数第二位获取性别  单数男/双数女
     *
     * @return
     */
    public static String getGenderByIdCard(String idCard) {
        if (StringUtils.isEmpty(idCard)) {
            return "";
        }
        int sex = Integer.parseInt(idCard.substring(16, 17));
        return ((sex % 2 == 1) ? "1" : "0");
    }

    /**
     * 根据身份证获取年龄
     */
    public static int getAgeByIdCard(String idCard) {
        if (StringUtils.isEmpty(idCard)) {
            return 0;
        }
        String year = idCard.substring(6, 10);
        String month = idCard.substring(10, 12);
        String day = idCard.substring(12, 14);
        return DateUtil.ageOfNow(StrUtil.format("{}-{}-{}", year, month, day));
    }
}
